#include <tsin/library/BulletAutoRotateEvent.hpp>

#include <tsin/runtime/BulletRuntime.hpp>
#include <tsin/util/Logger.hpp>

#include <cstring>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace tsin;

BulletAutoRotateEvent::BulletAutoRotateEvent(bool d)
        : BulletEvent(d), tick(0), dangle(0.0f)
{
}

void BulletAutoRotateEvent::init(float dangle)
{
        this->dangle = dangle;
}

void BulletAutoRotateEvent::reinit()
{
        float arg1 = arguments[0]->getFloatInRad();

        switch(nrArguments) {
        case 0:
                init();
                break;
        case 1:
                if (strcmp(desp, "N") == 0)
                        init(arg1);
                break;
        }
}

DynamicObject *BulletAutoRotateEvent::copy()
{
        return DynamicObject::copy(new BulletAutoRotateEvent(true));
}

bool BulletAutoRotateEvent::trigger(BulletRuntime &bullet, void *)
{
        return bullet.stage == BULLET_STAGE_LIVE;
}

BulletEvent *BulletAutoRotateEvent::handle(BulletRuntime &bullet, void *)
{
        bullet.angle = bullet.direction + tick * dangle;
        tick ++;
        return this;
}

tsin::DynamicObject *bindAutoRotateEvent()
{
        return new BulletAutoRotateEvent(true);
}
